package com.qf.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/admin/storage")
public class AdminStorageController {

    @Autowired
    DtsStorageService storageService;

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsStorage storage){
        storageService.delete(storage);

        return ResponseUtil.ok();
    }

    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                       @RequestParam(value = "limit",required = false,defaultValue = "10")Integer limit,
                       @RequestParam(value = "sort",required = false,defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",required = false,defaultValue = "desc")String order,
                       String name,String key){

        Map<String, Object> data = storageService.findByPage(page, limit, sort, order, name, key);

        return ResponseUtil.ok(data);
    }

    @PostMapping("/create")
    public Object create(MultipartFile file) throws Exception {
        //获取文件名---有后缀
        String filename = file.getOriginalFilename();
        //文件大小
        long size = file.getSize();
        //类型
        String type = file.getContentType();
        //唯一名称
        String uuid = UUID.randomUUID().toString();
        String objectName = uuid + filename;

        DtsStorage storage =storageService.uploadFile(filename, (int) size,type,objectName,file.getBytes());
        return ResponseUtil.ok(storage);

    }
}
